﻿using Newtonsoft.Json;
using SE.PO.CInterface.DBProtocols.Devices;
using SE.PO.CInterface.Po.Equips;

namespace SE.PO.CInterface.Web.Clients.Po;

public class EquipClient : CurdClientBase<string, DeviceDto, CreateUpdateDeviceDto>
{
    public override string GetRoute() => "equips";

    public EquipClient(IHttpClientFactory clientFactory, IConfiguration configuration) : base(clientFactory,configuration)
    {
    }

    public new async Task<PageData<EquipmentDto>?> Get(string name,string? deviceType)
    {
        var client = ClientFactory.CreateClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{BaseUrl}/api/app/equips?name={name}&deviceType={deviceType}"),
        };
        using var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();
        var items = JsonConvert.DeserializeObject<EquipmentDto[]>(body);
        if (items != null)
        {
            var pageData = new PageData<EquipmentDto>
            {
                TotalCount = items.Length,
                Items = items
            };
            Console.WriteLine(body);
            return pageData;
        }

        return default;
    }

}